package Admin;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JPasswordField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ChatLogin extends JPanel {
	
	Chat_Internal_Frame cif;
	String irum;
	
	private JTextField tf_id;
	private JButton btn_login;
	private JLabel lblNewLabel;
	private JLabel lblPw;
	private JPasswordField tf_pwd;
	private JLabel lb_gong;

	/**
	 * Create the panel.
	 */
	public ChatLogin() {
		setLayout(null);
		add(getTf_id());
		add(getBtn_login());
		add(getLblNewLabel());
		add(getLblPw());
		add(getTf_pwd());
		add(getLb_gong());

	}
	public ChatLogin(Chat_Internal_Frame cif)
	{
		this();
		this.cif = cif;
	}
	public ChatLogin(Chat_Internal_Frame cif, String toid)
	{
		this();
		this.cif = cif;
		tf_id.setText(toid);
		tf_id.setEditable(false);
		tf_pwd.requestFocus();
	}
	
	public JTextField getTf_id() {
		if (tf_id == null) {
			tf_id = new JTextField();
			tf_id.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent arg0) {
					tf_pwd.requestFocus();
				}
			});
			tf_id.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent ke) {
					if(ke.getKeyCode()==ke.VK_ENTER || ke.getKeyCode()==ke.VK_TAB)
					{
						tf_id.setFocusable(false);
//						tf_pwd.requestFocus();
						tf_id.setFocusable(true);
					}
				}
			});
			tf_id.setFont(new Font("굴림", Font.BOLD, 24));
			tf_id.setBounds(90, 10, 147, 35);
			tf_id.setColumns(10);
		}
		return tf_id;
	}
	public JButton getBtn_login() {
		if (btn_login == null) {
			btn_login = new JButton("Login");
			btn_login.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					checkid();
				}
			});
			btn_login.setFont(new Font("굴림", Font.BOLD, 40));
			btn_login.setBounds(249, 10, 147, 86);
		}
		return btn_login;
	}
	public JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("ID");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setFont(new Font("굴림", Font.BOLD, 30));
			lblNewLabel.setBounds(12, 10, 66, 35);
		}
		return lblNewLabel;
	}
	public JLabel getLblPw() {
		if (lblPw == null) {
			lblPw = new JLabel("PW");
			lblPw.setHorizontalAlignment(SwingConstants.CENTER);
			lblPw.setFont(new Font("굴림", Font.BOLD, 30));
			lblPw.setBounds(12, 61, 66, 35);
		}
		return lblPw;
	}
	public JPasswordField getTf_pwd() {
		if (tf_pwd == null) {
			tf_pwd = new JPasswordField();
			tf_pwd.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode()==10)
					{
						checkid();
					}
				}
			});
			tf_pwd.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent arg0) {
					btn_login.requestFocus();
				}
			});
			tf_pwd.setFont(new Font("굴림", Font.BOLD, 24));
			tf_pwd.setBounds(90, 61, 147, 35);
		}
		return tf_pwd;
	}
	public void checkid()
	{
		Connection conn = new MySqlDB().getConn();
		int flag = -1;
		
		String sql = "select pwd, grade, irum from member where mid=?";
		try
		{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, tf_id.getText());
			
			ResultSet rs = pst.executeQuery();
			
			if(rs.next())
			{
				String pwd = rs.getString("pwd");
				String grade = rs.getString("grade");
//				irum=rs.getString("irum");
				cif.setIrum(rs.getString("irum"));
				if(tf_pwd.getText().equals(pwd))
				{
					flag = 1;
					if(grade.equals("2"))
					{
						flag = 1;
					}
					else
					{
						flag = 4;
					}
				}
				else
				{
					flag = 2;
				}				
			}
			else
			{
				flag=3;
			}
			pst.close();
			rs.close();
			conn.close();
			switch(flag)
			{
			case 1:
				cif.setLogin_id(tf_id.getText());
				if(tf_id.isEditable())
				{
					cif.designer();
				}
				cif.logon();
				break;
			case 2:
				lb_gong.setText("비밀번호 틀림");
				lb_gong.setForeground(Color.red);
				break;
			case 3:
				lb_gong.setText("아이디 틀림");
				lb_gong.setForeground(Color.red);
				break;
			case 4:
				lb_gong.setText("디자이너가 아닙니다.");
				lb_gong.setForeground(Color.red);
				break;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public JLabel getLb_gong() {
		if (lb_gong == null) {
			lb_gong = new JLabel("로그인을 하세요.");
			lb_gong.setFont(new Font("굴림", Font.BOLD, 30));
			lb_gong.setHorizontalAlignment(SwingConstants.CENTER);
			lb_gong.setBounds(12, 106, 384, 86);
		}
		return lb_gong;
	}
}
